package com.kaishengit.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.InventoryDao;
import com.kaishengit.dao.SalesDao;
import com.kaishengit.dao.SalesRecordsDao;
import com.kaishengit.pojo.Inventory;
import com.kaishengit.pojo.Sales;
import com.kaishengit.pojo.SalesRecords;

@Service
@Transactional
public class SalesService {
	private SalesDao salesDao;
	private InventoryDao inventoryDao;
	private SalesRecordsDao salesRecordsDao;
	
	public void add(Sales sales){
		Inventory inventory = inventoryDao.findByGoodsid(sales.getGoods().getId());
		inventory.setNumber(inventory.getNumber() - sales.getSalesnum());
		inventory.setGoods(sales.getGoods());
		inventoryDao.update(inventory);
		
		SalesRecords salesRecords = salesRecordsDao.findBySales(sales);
		if(salesRecords == null){
			SalesRecords sr = new SalesRecords();
			sr.setNumber(sales.getSalesnum());
			sr.setSalesprice(sales.getSalesprice());
			sr.setGoods(sales.getGoods());
			sr.setCustomer(sales.getCustomer());
			
			salesRecordsDao.add(sr);
		}else{
			salesRecords.setNumber(salesRecords.getNumber() + sales.getSalesnum());
			salesRecords.setGoods(sales.getGoods());
			salesRecords.setCustomer(sales.getCustomer());
			salesRecords.setSalesprice(sales.getSalesprice());
			
			salesRecordsDao.update(salesRecords);
		}
		
		salesDao.add(sales);
	}
	public List<Sales> findAll(){
		return salesDao.findAll();
	}
	public List<Sales> findByWhere(Sales sales){
		return salesDao.findByWhere(sales);
	}
	@Autowired
	public void setSalesDao(SalesDao salesDao) {
		this.salesDao = salesDao;
	}
	@Autowired
	public void setInventoryDao(InventoryDao inventoryDao) {
		this.inventoryDao = inventoryDao;
	}
	@Autowired
	public void setSalesRecordsDao(SalesRecordsDao salesRecordsDao) {
		this.salesRecordsDao = salesRecordsDao;
	}
	

}
